package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.beans.DolistItemBean;
import com.hxkcloud.patient.beans.NoticeFunctionBean;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.viewholders.DolistPreviewViewholder;

import java.util.List;

/**
 * Created by william_sun on 2016-07-12.
 * Description:用户执行单预览
 */
public class DolistPreviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<DolistItemBean> datas;
    private LayoutInflater mInflater;
    private NoticeDoAdapter.onDolistFunctionListener listener;
    private int type = 0;//0执行单 1提醒单

    public DolistPreviewAdapter(Context mContext, List<DolistItemBean> datas, int t, NoticeDoAdapter.onDolistFunctionListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.type = t;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_noticedo_info, null);
        DolistPreviewViewholder holder = new DolistPreviewViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DolistPreviewViewholder vhInfo = (DolistPreviewViewholder) holder;

        vhInfo.ll_func.setVisibility(View.GONE);
        vhInfo.ll_child.setVisibility(View.VISIBLE);

        DolistItemBean item = datas.get(position);

        if (position == 0) {
            vhInfo.view_line1.setVisibility(View.INVISIBLE);
            vhInfo.view_line2.setVisibility(View.VISIBLE);
        } else if (position == datas.size() - 1) {
            vhInfo.view_line1.setVisibility(View.VISIBLE);
            vhInfo.view_line2.setVisibility(View.INVISIBLE);
        } else {
            vhInfo.view_line1.setVisibility(View.VISIBLE);
            vhInfo.view_line2.setVisibility(View.VISIBLE);
        }

        vhInfo.view_line1.getLayoutParams().height = ImageUtil.dip2px(25, mContext);
        vhInfo.ll_reminddate.setVisibility(View.GONE);

        vhInfo.ll_items.removeAllViews();
        vhInfo.ll_items.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout ll_itemss = new LinearLayout(mContext);
        LinearLayout.LayoutParams lp_itemss = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp_itemss.weight = 1;
        ll_itemss.setLayoutParams(lp_itemss);
        ll_itemss.setOrientation(LinearLayout.VERTICAL);

        for (NoticeFunctionBean func : item.getItemList()) {
            TextView tv_function = new TextView(mContext);
            LinearLayout.LayoutParams lp_function = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tv_function.setLayoutParams(lp_function);
            tv_function.setTextSize(14);
            tv_function.setLineSpacing(ImageUtil.dip2px(5, mContext), 1);
            tv_function.setPadding(0, ImageUtil.dip2px(2, mContext), 0, ImageUtil.dip2px(2, mContext));
//            tv_function.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tv_function.setText(func.getContent());
            if (!func.getType().equals(YjhApplication.doorder_type_content)) {
                tv_function.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                tv_function.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
            } else {
                tv_function.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
            }
            if (func.getType().equals(YjhApplication.doorder_type_article)) {
                tv_function.setTag(func.getItemId());
                tv_function.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.article((String) v.getTag());
                    }
                });
            }
            ll_itemss.addView(tv_function);
        }
        vhInfo.ll_items.addView(ll_itemss);
        if (type == 1) {
            TextView tv_day = new TextView(mContext);
            LinearLayout.LayoutParams lp_function = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            lp_function.leftMargin = ImageUtil.dip2px(5, mContext);
            tv_day.setGravity(Gravity.CENTER);
            tv_day.setLayoutParams(lp_function);
            tv_day.setTextSize(18);
            tv_day.setText("第" + item.getDay() + "天");
            vhInfo.ll_items.addView(tv_day);
        }
//        if (!TextUtils.isEmpty(item.getContents())) {
//            TextView tv_content = new TextView(mContext);
//            LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            tv_content.setLayoutParams(lp_tv);
//            tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
//            tv_content.setTextSize(16);
//            tv_content.setText(item.getContents());
//            vhInfo.ll_items.addView(tv_content);
//        }
//        for (NoticeFunctionBean function : item.getCheckIds()) {
//            TextView tv_function = new TextView(mContext);
//            LinearLayout.LayoutParams lp_function = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            tv_function.setLayoutParams(lp_function);
//            tv_function.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
//            //http://blog.sina.com.cn/s/blog_671d2e4f0101f8f7.html
//            tv_function.setTextSize(16);
//            tv_function.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//            tv_function.setText(function.getMonitorName());
//            vhInfo.ll_items.addView(tv_function);
////                tv_function.setTag(function.getMonitorId());
////                tv_function.setOnClickListener(new View.OnClickListener() {
////                    @Override
////                    public void onClick(View v) {
////                        listener.check((String) v.getTag());
////                    }
////                });
//        }
//        for (DolistArticleBean article : item.getArticleIds()) {
//            TextView tv_article = new TextView(mContext);
//            LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            tv_article.setLayoutParams(lp_tv);
//            tv_article.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
//            tv_article.setTextSize(16);
//            tv_article.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//            tv_article.setText(article.getArticleName());
//            vhInfo.ll_items.addView(tv_article);
//            tv_article.setTag(article.getArticleId());
//            tv_article.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    listener.article((String) v.getTag());
//                }
//            });
//        }
        vhInfo.ll_times1.removeAllViews();
        vhInfo.ll_times2.removeAllViews();
        for (int i = 0; i < Math.min(4, item.getTimeIds().size()); i++) {
            String time = item.getTimeIds().get(i).getTime();
            TextView tv_time = new TextView(mContext);
            LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp_time.rightMargin = ImageUtil.dip2px(8, mContext);
            tv_time.setLayoutParams(lp_time);
            tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
            tv_time.setTextSize(14);
            tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
            tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
            tv_time.setText(time);
            vhInfo.ll_times1.addView(tv_time);
        }
        if (item.getTimeIds().size() > 4) {
            vhInfo.ll_times2.setVisibility(View.VISIBLE);
            for (int i = 4; i < item.getTimeIds().size(); i++) {
                String time = item.getTimeIds().get(i).getTime();
                TextView tv_time = new TextView(mContext);
                LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp_time.rightMargin = ImageUtil.dip2px(8, mContext);
                tv_time.setLayoutParams(lp_time);
                tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                tv_time.setTextSize(14);
                tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                tv_time.setText(time);
                vhInfo.ll_times2.addView(tv_time);
            }
        } else {
            vhInfo.ll_times2.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
}
